package com.zzw.aliyun.green.config;

import com.zzw.aliyun.green.common.constants.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({AliyunGreenConfigProperties.class})
@ComponentScan(basePackages = {"com.zzw.aliyun.green.service"})
public class AliyunGreenAutoConfiguration {

    /**
     * 创建阿里云 增强版green client
     *
     * @return
     * @throws Exception
     **/
    @Bean
    public com.aliyun.green20220302.Client createClient(AliyunGreenConfigProperties aliyunGreenConfigProperties) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(aliyunGreenConfigProperties.getAccessKeyId())
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(aliyunGreenConfigProperties.getSecret());
        // Endpoint 请参考 https://api.aliyun.com/product/Green
        config.endpoint = getRegionUrl(aliyunGreenConfigProperties.getRegion());
        return new com.aliyun.green20220302.Client(config);
    }

    private String getRegionUrl(String region){
        switch (region){
            case "cn-beijing":
                return Region.CN_BEIJING;
            case "ap-southeast-1":
                return Region.CN_SOUTHEAST_1;
            default:
                return Region.CN_SHANGHAI;
        }
    }

}
